package com.jp.game.utils;

import android.content.Context;
import android.widget.Toast;

/**
 * toast 工具类 保证界面上只有一个toast正在显示
 * 上一个没有小时前继续调用show会覆盖上一个
 * @author Administrator
 *
 */
public class ToastUtil
{
    public static byte[] lock = new byte[0]; 
    public static Toast toast;
    
    public static void show(Context context, String text){
        if(toast == null){
            synchronized (lock)
            {
                if(toast == null){
                    toast = Toast.makeText(context.getApplicationContext(), text, Toast.LENGTH_SHORT);
                }
            }
        }
        
        toast.setText(text);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }
    
    public static void show(Context context, int resId){
        if(toast == null){
            synchronized (lock)
            {
                if(toast == null){
                    toast = Toast.makeText(context.getApplicationContext(), resId, Toast.LENGTH_SHORT);
                }
            }
        }
        toast.setText(resId);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

    public static void show(Context context, int resId, int duration){
        if(toast == null){
            synchronized (lock)
            {
                if(toast == null){
                    toast = Toast.makeText(context.getApplicationContext(), resId, duration);
                }
            }
        }
        toast.setText(resId);
        toast.setDuration(duration);
        toast.show();
    }
    
}
